<?php

namespace App\Admin\Controllers;

use App\Models\Image;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Grid\Filter;
use Encore\Admin\Grid\Tools\BatchActions;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;
use Encore\Admin\Grid\Tools;

class LogoController extends Controller
{
	use HasResourceActions;

	/**
	 * Index interface.
	 *
	 * @param Content $content
	 *
	 * @return Content
	 */
	public function index(Content $content)
	{
		return $content->header('图片管理')->description('图片列表')->body($this->grid());
	}

	/**
	 * Show interface.
	 *
	 * @param mixed   $id
	 * @param Content $content
	 *
	 * @return Content
	 */
	public function show($id, Content $content)
	{
		return $content->header('图片查看')->description('图片')->body($this->detail($id));
	}

	/**
	 * Edit interface.
	 *
	 * @param mixed   $id
	 * @param Content $content
	 *
	 * @return Content
	 */
	public function edit($id, Content $content)
	{
		return $content->header('图片编辑')->description('图片')->body($this->form()->edit($id));
	}

	/**
	 * Create interface.
	 *
	 * @param Content $content
	 *
	 * @return Content
	 */
	public function create(Content $content)
	{
		return $content->header('添加')->description('添加图片')->body($this->form());
	}

	/**
	 * Make a grid builder.
	 *
	 * @return Grid
	 */
	protected function grid()
	{
		$grid = new Grid(new Image);

		$grid->id('Id');
		$grid->url('图片')->lightbox(['width' => 80, 'height' => 80]);
        $grid->shelf('上下架')->radio([
			'0' => '下架',
			'1' => '上架'
		]);
        $grid->created_at('创建时间');
		$grid->updated_at('编辑时间');

        $grid->filter(function(Filter $filter) {
            $filter->equal('shelf', '上下架')->select([
                '0' => '下架',
                '1' => '上架'
            ]);
        });

		$grid->model()->orderByDesc('id');
		$grid->disableExport();
		$grid->disableRowSelector();
		return $grid;
	}

	/**
	 * Make a show builder.
	 *
	 * @param mixed $id
	 *
	 * @return Show
	 */
	protected function detail($id)
	{
		$show = new Show(Image::findOrFail($id));

		$show->id('Id');
		$show->url('Url')->image();
		$show->comment('备注');
		$show->shelf('上下架')->using(['0' => '下架', '1' => '上架']);
		$show->created_at('创建时间');
		$show->updated_at('编辑时间');

		$show->panel()->tools(function(Show\Tools $tools) {
			$tools->disableDelete();
		});

		return $show;
	}

	/**
	 * Make a form builder.
	 *
	 * @return Form
	 */
	protected function form()
	{
		$form = new Form(new Image);

		$form->image('url', '图片')->uniqueName();
		$form->radio('shelf', '上下架')->options([
			'0' => '下架',
			'1' => '上架'
		])->default('0');

		return $form;
	}
}
